//
//  UserDataEditLabelViewController.m
//  Molian
//
//  Created by 胡津 on 15/4/8.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "UserDataEditLabelViewController.h"
#import "PlaceholderTextView.h"

@interface UserDataEditLabelViewController ()<UITextViewDelegate>

@end

@implementation UserDataEditLabelViewController
{
    PlaceholderTextView *TextView;
    BOOL reset;
    NSInteger textCount;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    
    reset = YES;
    
    TextView.clipsToBounds = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    TextView=[[PlaceholderTextView alloc] initWithFrame:CGRectMake(10, 70, DeviceWidth -20, 100)];
    TextView.placeholder = [NSString stringWithFormat:@"请输入%@",self.title];
    TextView.font = [UIFont fontWithName:FontName size:13];
    TextView.placeholderFont = [UIFont fontWithName:FontName size:14];
    TextView.layer.borderWidth=0.5;
    TextView.layer.borderColor=[UIColor lightGrayColor].CGColor;
    TextView.layer.masksToBounds = YES;//开启圆角
    [TextView.layer setCornerRadius:7];
    TextView.placeholderColor=[UIColor grayColor];
    [self.view addSubview:TextView];
    TextView.delegate = self;
    TextView.label.frame = CGRectMake(CGRectGetWidth(TextView.frame)-200, CGRectGetHeight(TextView.frame)+70, 200, 20);
    [self.view addSubview:TextView.label];

    
    [TextView becomeFirstResponder];
    
    textCount = 320;
    
    [TextView addMaxTextLengthWithMaxLength:textCount andEvent:^(PlaceholderTextView *text) {
        NSLog(@"----------");
    }];
    
    [TextView addTextViewBeginEvent:^(PlaceholderTextView *text) {
        NSLog(@"begin");
    }];
    
    [TextView addTextViewEndEvent:^(PlaceholderTextView *text) {
        NSLog(@"end");
    }];
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ( [text isEqualToString:@"\n"] ) {
        [textView resignFirstResponder];
        return NO;
    }else{
        return YES;
    }
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    if (reset == YES) {
        TextView.text = _currentStr;
        reset = NO;
    }
    if ([self.title isEqualToString:@"昵称"]) {
        TextView.maxTextLength = 20;
    }else if([self.title isEqualToString:@"职业"]){
        TextView.maxTextLength = 32;
    }else{
        TextView.maxTextLength = 320;
    }
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    if ([self.Delegate respondsToSelector:@selector(UserString:)]) {
        if (TextView.text.length > textCount) {
            NSString *b = [TextView.text substringToIndex:textCount];
            TextView.text = b;
        }
        [self.Delegate UserString:TextView.text];
        reset = NO;
    }
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    reset = YES;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}
- (void)dealloc
{
    self.delegate = nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
